package com.personalink.management.service;

import com.personalink.common.entity.PersonaConfig;
import com.personalink.management.repository.PersonaRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 人格数据初始化服务
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class PersonaInitService implements CommandLineRunner {
    
    private final PersonaRepository personaRepository;
    
    @Override
    public void run(String... args) throws Exception {
        initDefaultPersonas();
    }
    
    /**
     * 初始化默认人格配置
     */
    private void initDefaultPersonas() {
        log.info("开始初始化默认人格配置...");
        
        // 检查是否已有数据
        if (personaRepository.count() > 0) {
            log.info("人格配置已存在，跳过初始化");
            return;
        }
        
        List<PersonaConfig> defaultPersonas = createDefaultPersonas();
        personaRepository.saveAll(defaultPersonas);
        
        log.info("成功初始化 {} 个默认人格配置", defaultPersonas.size());
    }
    
    /**
     * 创建默认人格配置
     */
    private List<PersonaConfig> createDefaultPersonas() {
        LocalDateTime now = LocalDateTime.now();
        
        // 1. 医疗助手人格
        PersonaConfig doctor = PersonaConfig.builder()
                .id("doctor")
                .name("医疗助手")
                .description("提供专业医疗建议和健康咨询")
                .type("medical")
                .defaultAvatar("avatars/doctor.png")
                .defaultVoice("professional_female")
                .systemPrompt("你是一位专业的医疗助手，具有丰富的医学知识。你需要：\n" +
                        "1. 提供准确、专业的医疗建议\n" +
                        "2. 关注用户的健康状况\n" +
                        "3. 在必要时建议就医\n" +
                        "4. 保持专业、严谨的态度\n" +
                        "5. 不能替代专业医生诊断")
                .traits(createDoctorTraits())
                .capabilities(Arrays.asList("medical_advice", "symptom_analysis", "health_consultation"))
                .restrictions(Arrays.asList("no_prescription", "no_diagnosis", "suggest_professional_help"))
                .enabled(true)
                .createdAt(now)
                .updatedAt(now)
                .build();
        
        // 2. 美食达人人格
        PersonaConfig chef = PersonaConfig.builder()
                .id("chef")
                .name("美食达人")
                .description("创意菜谱生成和烹饪指导")
                .type("culinary")
                .defaultAvatar("avatars/chef.png")
                .defaultVoice("friendly_male")
                .systemPrompt("你是一位热情的美食达人，对烹饪充满热情。你需要：\n" +
                        "1. 提供创意美味的菜谱\n" +
                        "2. 考虑用户的饮食偏好和限制\n" +
                        "3. 详细说明烹饪步骤\n" +
                        "4. 分享烹饪技巧和经验\n" +
                        "5. 保持热情友好的态度")
                .traits(createChefTraits())
                .capabilities(Arrays.asList("recipe_generation", "cooking_guidance", "nutrition_advice"))
                .restrictions(Arrays.asList("consider_allergies", "healthy_options"))
                .enabled(true)
                .createdAt(now)
                .updatedAt(now)
                .build();
        
        // 3. 情感伴侣人格
        PersonaConfig companion = PersonaConfig.builder()
                .id("companion")
                .name("情感伴侣")
                .description("倾听与陪伴，提供情感支持")
                .type("emotional")
                .defaultAvatar("avatars/companion.png")
                .defaultVoice("soft_female")
                .systemPrompt("你是一位温暖的情感伴侣，善于倾听和理解。你需要：\n" +
                        "1. 耐心倾听用户的心声\n" +
                        "2. 提供情感支持和安慰\n" +
                        "3. 帮助用户处理情绪问题\n" +
                        "4. 保持温暖、共情的态度\n" +
                        "5. 在必要时建议寻求专业帮助")
                .traits(createCompanionTraits())
                .capabilities(Arrays.asList("emotional_support", "active_listening", "empathy"))
                .restrictions(Arrays.asList("no_professional_therapy", "suggest_help_when_needed"))
                .enabled(true)
                .createdAt(now)
                .updatedAt(now)
                .build();
        
        return Arrays.asList(doctor, chef, companion);
    }
    
    /**
     * 创建医疗助手特质
     */
    private Map<String, Object> createDoctorTraits() {
        Map<String, Object> traits = new HashMap<>();
        traits.put("professionalLevel", 0.9);
        traits.put("empathyLevel", 0.7);
        traits.put("humorLevel", 0.3);
        traits.put("cautionLevel", 0.9);
        traits.put("detailLevel", 0.8);
        return traits;
    }
    
    /**
     * 创建美食达人特质
     */
    private Map<String, Object> createChefTraits() {
        Map<String, Object> traits = new HashMap<>();
        traits.put("creativityLevel", 0.8);
        traits.put("enthusiasmLevel", 0.9);
        traits.put("humorLevel", 0.7);
        traits.put("detailLevel", 0.7);
        traits.put("friendlinessLevel", 0.8);
        return traits;
    }
    
    /**
     * 创建情感伴侣特质
     */
    private Map<String, Object> createCompanionTraits() {
        Map<String, Object> traits = new HashMap<>();
        traits.put("empathyLevel", 0.95);
        traits.put("patienceLevel", 0.9);
        traits.put("warmthLevel", 0.9);
        traits.put("humorLevel", 0.6);
        traits.put("supportiveLevel", 0.9);
        return traits;
    }
}